Imports System.Drawing
Imports System.Windows.Forms
Imports Vintasoft.Imaging

Namespace Imaging
	Public Partial Class PaletteForm
		Inherits Form

		#Region "Fields"

		Private _showingColor As Boolean = False

		#End Region



		#Region "Constructor"

		Public Sub New()
			InitializeComponent()
		End Sub

		#End Region



		#Region "Properties"

		''' <summary>
		''' Gets the <see cref="PaletteViewer"/>.
		''' </summary>
		Public ReadOnly Property PaletteViewer() As PaletteViewer
			Get
				Return paletteViewer1
			End Get
		End Property

		''' <summary>
		''' Gets value indicating whether the <see cref="PaletteViewer"/> can change palette.
		''' </summary>
		Public Property CanChangePalette() As Boolean
			Get
				Return PaletteViewer.CanChangePalette
			End Get
			Set
				PaletteViewer.CanChangePalette = value
				toGrayButton.Visible = value
				invertButton.Visible = value
			End Set
		End Property

		#End Region



		#Region "Methods"

		Protected Overrides Sub OnShown(e As EventArgs)
			MyBase.OnShown(e)
			If Not PaletteViewer.CanChangePalette Then
				toGrayButton.Visible = False
				invertButton.Visible = False
				alphaNumericUpDown.Enabled = False
				redNumericUpDown.Enabled = False
				greenNumericUpDown.Enabled = False

				blueNumericUpDown.Enabled = False
			End If
			colorIndexNumericUpDown.Maximum = PaletteViewer.Palette.ColorCount - 1
			UpdateColor()
		End Sub

		Private Sub PaletteViewer_SelectedColorChanged(sender As Object, e As EventArgs)
			UpdateColor()
		End Sub

		Private Sub UpdateColor()
			Dim selectedColor As Color = PaletteViewer.SelectedColor
			_showingColor = True
			alphaNumericUpDown.Value = selectedColor.A
			redNumericUpDown.Value = selectedColor.R
			greenNumericUpDown.Value = selectedColor.G
			blueNumericUpDown.Value = selectedColor.B
			colorIndexNumericUpDown.Value = PaletteViewer.SelectedColorIndex
			_showingColor = False
			UpdateTitle()
		End Sub

		Private Sub UpdateTitle()
			Dim selectedColor As Color = PaletteViewer.SelectedColor
			Text = String.Format("Palette: Selected index = {0}; RGB({1},{2},{3})", PaletteViewer.SelectedColorIndex, selectedColor.R, selectedColor.G, selectedColor.B)
		End Sub

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.OK
		End Sub

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.Cancel
		End Sub

		Private Sub invertButton_Click(sender As Object, e As EventArgs)
			PaletteViewer.Palette.Invert()
		End Sub

		Private Sub toGrayButton_Click(sender As Object, e As EventArgs)
			PaletteViewer.Palette.ConvertToGrayColors()
		End Sub

		Private Sub colorNumericUpDown_ValueChanged(sender As Object, e As EventArgs)
			If Not _showingColor Then
				Dim selectedColor As Color = Color.FromArgb(CInt(Math.Truncate(redNumericUpDown.Value)), CInt(Math.Truncate(greenNumericUpDown.Value)), CInt(Math.Truncate(blueNumericUpDown.Value)))
				selectedColor = Color.FromArgb(CInt(Math.Truncate(alphaNumericUpDown.Value)), selectedColor)
				PaletteViewer.SelectedColor = selectedColor
			End If
		End Sub

		Private Sub colorIndexNumericUpDown_ValueChanged(sender As Object, e As EventArgs)
			If Not _showingColor Then
				PaletteViewer.SelectedColorIndex = CByte(Math.Truncate(colorIndexNumericUpDown.Value))
			End If
		End Sub

		#End Region


	End Class
End Namespace
